package base.dao.impl;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import base.dao.BaseDao;
import common.annotation.mytable;
import common.db.DBUtil;
import common.reflect.ReflectUtil;
import tmall.company.entity.CompanyEntity;

/*Connection conn = DBUtil.getConn();
String sql="INSERT INTO tmall_company_t(NAME,ADDRESS,TELEPHONE,BOSS) VALUES (?,?,?,?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1,entity.getName());
stmt.setString(2,entity.getAddress());
stmt.setString(3,entity.getTelephone());
stmt.setString(4,entity.getBoss());
int count = stmt.executeUpdate();
return count;*/

public abstract class BaseDaoImpl2<T> implements BaseDao<T> {
	
	protected Class<T> entityClass;
	protected String className;
	
	public  BaseDaoImpl2(){
		 entityClass=(Class<T>) getClass().getGenericSuperclass().getClass();
		 className=entityClass.getName();
		 
	}
	
	
	//定义抽象方法，让子类实现
	public  abstract Connection getConnection();

	
	//去下划线
	public static String remove_(String str){
		String[] split = str.split("_");
		String newString="";
		for (String s : split) {
			newString+=s;
		}
		return newString;
	}

	@Override
	public int add(T entity) throws Exception {
//		String sql="INSERT INTO tmall_company_t(NAME,ADDRESS,TELEPHONE,BOSS) VALUES (?,?,?,?)";
		Class<? extends Object> clazz = entity.getClass();
		mytable annotation2 = clazz.getAnnotation(mytable.class);
		String tableName=annotation2.name();
		Field[] fields = entity.getClass().getDeclaredFields();
//		System.out.println(fields[0].getName());
		//(NAME,ADDRESS,TELEPHONE,BOSS)
		
		//拼接sql
		StringBuffer sb=new StringBuffer();
		String entitysString = ReflectUtil.getEntity(fields);
//		System.out.println(entitysString);
		String unkownString = ReflectUtil.getUnkown(fields);		
//		System.out.println(unkownString);
		String sql="INSERT INTO "+tableName+entitysString+" VALUES"+unkownString;
//		System.out.println(sql);
		Connection conn = getConnection();
		PreparedStatement stmt = conn.prepareStatement(sql);
		
		Method[] methods = clazz.getDeclaredMethods();
		
		//stmt.setString(1,entity.getName());
		/*for(int i=0;i<fields.length;i++){
			String fieldName = fields[i].getName();

			for (Method method : methods) {
				if(method.getName().contains("get")){
					
					if(method.getName().equalsIgnoreCase("get"+fieldName)){
						
						String ty = fields[i].getType().getName();
//						System.out.println(ty);
						Object invoke = method.invoke(entity);
						if(ty.equals("java.lang.String")){
							stmt.setString(i+1,(String)invoke);
							break;
						} else if(ty.equals("java.lang.Integer")){
							stmt.setInt(i+1,(int)invoke);
							break;
						}
					}
				}
			}
		}*/
		ReflectUtil.setParameter(fields, methods, stmt, entity);
		int counter = stmt.executeUpdate();
		return counter;
	}

	/*Connection conn = DBUtil.getConn();
	String sql="UPDATE tmall_company_t SET is_delete=0 WHERE id=?";
	DELETE from tmall_company_t WHERE id=111;
	PreparedStatement stmt = conn.prepareStatement(sql);
	stmt.setInt(1,id);
	int count = stmt.executeUpdate();
	return count;
	*/
	@Override
	public int deleteById(Integer id) throws Exception {
		//DELETE from tmall_company_t WHERE id=111;
		
		
		
		return 0;
	}

	@Override
	public int deleteById(String id) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int update(T t) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public T findById(Integer id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public T findById(String id) throws Exception {
//		SELECT * FROM `tmall_company_t` WHERE id=11;
		
		
		return null;
	}

	@Override
	public List<T> findAll() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<T> findWithPage(Integer currentPage, Integer pageSize) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
